package com.oa.user.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.oa.entity.ExtMsgResult;
import com.oa.entity.ExtPageable;
import com.oa.entity.ExtjsAjaxResult;
import com.oa.entity.Result;
import com.oa.log.annotation.Log;
import com.oa.system.entity.UserInfo;
import com.oa.user.dto.UserInfoDto;
import com.oa.user.service.UserManagerService;

@Controller
@RequestMapping("/user")
public class UserManagerController {
	@Resource
	private UserManagerService userManagerService;

	// user/updateUser 更新自己的用户信息
	@PostMapping("/updateUser")
	@Log(oprate = "更新自己的信息资料")
	@PreAuthorize("isAuthenticated()")
	public @ResponseBody ExtMsgResult updateUser(UserInfoDto userInfoDto) {
		if (userManagerService.updateUserInfo(userInfoDto)) {
			return new ExtMsgResult(200, "用户信息更新成功");
		} else {
			return new ExtMsgResult(400, "用户信息更新失败");
		}
	}

	// user/updateUserInfo 更新员工信息
	@PostMapping("/updateUserInfo")
	@Log(oprate = "更新员工信息")
	@PreAuthorize("hasAnyAuthority('ADMIN','HIGH_MANAGER')")
	public @ResponseBody ExtMsgResult updateUserInfo(UserInfoDto userInfoDto) {
		if (userManagerService.updateUserInfo(userInfoDto)) {
			return new ExtMsgResult(200, "用户信息更新成功");
		} else {
			return new ExtMsgResult(400, "用户信息更新失败");
		}
	}

	// user/uploadCover 上传头像
	@PostMapping("/uploadCover")
	@Log(oprate = "上传头像")
	@PreAuthorize("isAuthenticated()")
	public @ResponseBody ExtjsAjaxResult uploadCover(@RequestParam("cover") MultipartFile multipartFile) {
		try {
			userManagerService.saveUserCover(multipartFile);
			return new ExtjsAjaxResult(true, "上传头像成功");
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
			return new ExtjsAjaxResult(false, "上传头像失败");
		}
	}

	// user/deleteUser 删除用户信息
	@PostMapping("/deleteUser")
	@Log(oprate = "删除员工信息")
	@PreAuthorize("hasAnyAuthority('ADMIN','HIGH_MANAGER')")
	public @ResponseBody ExtMsgResult deleteUser(@RequestParam("id") Long id) {
		if (userManagerService.deleteUser(id)) {
			return new ExtMsgResult(200, "删除用户信息成功");
		} else {
			return new ExtMsgResult(404, "删除用户信息失败");
		}
	}

	// user/deleteUser 删除多个用户信息
	@PostMapping("/deleteUsers")
	@Log(oprate = "删除员工信息")
	@PreAuthorize("hasAnyAuthority('ADMIN','HIGH_MANAGER')")
	public @ResponseBody ExtMsgResult deleteUsers(@RequestParam("ids") List<Long> ids) {
		if (userManagerService.deleteUser(ids)) {
			return new ExtMsgResult(200, "批量删除用户信息成功");
		} else {
			return new ExtMsgResult(400, "批量删除用户信息失败");
		}
	}

	// user/findByDeptId 获取某部门的全部用户信息
	@GetMapping("/findByDeptId")
	public @ResponseBody Result<List<UserInfoDto>> findByDeptId(@RequestParam("deptId") Long deptId) {
		return new Result<>(200, "", userManagerService.findByDeptId(deptId));
	}

	// user/all 获取分页的全部用户信息
	@GetMapping("/all")
	public @ResponseBody Page<UserInfo> getAllUserInfo(ExtPageable extPageable) {
		return userManagerService.getAllUserInfo(extPageable.getPageable());
	}

	// user/searchUser 查找符合条件的用户信息
	@GetMapping("/searchUser")
	public @ResponseBody Page<UserInfoDto> searchUser(UserInfoDto userInfoDto, ExtPageable extPageable) {
		return userManagerService.getUserInfoByQuery(userInfoDto, extPageable.getPageable());
	}

	// user/id/{id} 通过id查找用户信息
	@GetMapping("/id/{id}")
	public @ResponseBody Result<UserInfoDto> getUserInfo(@PathVariable("id") Long id) {
		return new Result<UserInfoDto>(200, "getUserInfoByID", userManagerService.getUserInfo(id));
	}

}
